// Copyright (C) 2025, ZWSOFT Co., LTD.(Guangzhou)
// Licensed under the MIT License. See LICENSE in the project root for license information.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using TestClient.Domain;

namespace TestClient.ViewModel
{
    public partial class IntersectionTestViewModel : PageViewModelBase
    {
        public IAsyncRelayCommand SelectCurve1Command { get; }
        public IAsyncRelayCommand SelectCurve2Command { get; }
        public IAsyncRelayCommand SelectSurface1Command { get; }
        public IAsyncRelayCommand SelectSurface2Command { get; }
        public IAsyncRelayCommand RunCCTestCommand { get; }
        public IAsyncRelayCommand RunCSTestCommand { get; }
        public IAsyncRelayCommand RunSSTestCommand { get; }

        [ObservableProperty]
        private string coordinate = "";
        [ObservableProperty]
        private string entId = "";

        #region test input
        [ObservableProperty]
        private string curveId1 = "";
        [ObservableProperty]
        private string curveId2 = "";
        [ObservableProperty]
        private string surfaceId1 = "";
        [ObservableProperty]
        private string surfaceId2 = "";

        public IntersectionTestViewModel(
            VFAdapterWrapper wrapper,
            ISnackNotifyService notifyService
        ) : base(wrapper, notifyService)
        {
            SelectCurve1Command = CreateAsyncRpcCommand(SelectCurve1);
            SelectCurve2Command = CreateAsyncRpcCommand(SelectCurve2);
            SelectSurface1Command = CreateAsyncRpcCommand(SelectSurface1);
            SelectSurface2Command = CreateAsyncRpcCommand(SelectSurface2);
            RunCCTestCommand = CreateAsyncRpcCommand(RunCCTest);
            RunCSTestCommand = CreateAsyncRpcCommand(RunCSTest);
            RunSSTestCommand = CreateAsyncRpcCommand(RunSSTest);
        }
        #endregion

        private async Task SelectCurve1()
        {
            EnsureConnected();
            CurveId1 = await ViewModelHelpers.SelectEnt(Wrapper, "Curve");
        }

        private async Task SelectCurve2()
        {
            EnsureConnected();
            CurveId2 = await ViewModelHelpers.SelectEnt(Wrapper, "Curve");
        }

        private async Task SelectSurface1()
        {
            EnsureConnected();
            SurfaceId1 = await ViewModelHelpers.SelectEnt(Wrapper, "Surface");
        }

        private async Task SelectSurface2()
        {
            EnsureConnected();
            SurfaceId2 = await ViewModelHelpers.SelectEnt(Wrapper, "Surface");
        }

        private async Task RunCCTest()
        {
            EnsureConnected();
            var (pointId, coord) = await Task.Run(() =>
            {
                var pointId = Wrapper.CurveCurveIntersection(CurveId1, CurveId2, out var coord);
                return (pointId, coord);
            });

            EntId = pointId;
            Coordinate = coord.ToString();
        }

        private async Task RunCSTest()
        {
            EnsureConnected();
            var (pointId, coord) = await Task.Run(() =>
            {
                var pointId = Wrapper.CurveSurfaceIntersection(CurveId1, SurfaceId1, out var coord);
                return (pointId, coord);
            });

            EntId = pointId;
            Coordinate = coord.ToString();
        }

        private async Task RunSSTest()
        {
            EnsureConnected();
            var entId = await Task.Run(() =>
            {
                return Wrapper.CreateCurveBySurfaceIntersection(SurfaceId1, SurfaceId2);
            });

            EntId = entId;
            Coordinate = "N/A";
        }
    }
}
